<script lang="ts" setup>
import { onLaunch } from "@dcloudio/uni-app";
// #ifdef APP-PLUS
const jpushModule = uni.requireNativePlugin("JG-JPush");
// #endif
function getRegistrationID() {
  //获取registerID
  jpushModule.getRegistrationID((result: any) => {
    let registerID = result.registerID;
    uni.setStorageSync("registerID", registerID);
  });
}
function getNotificationEnabled() {
  if (uni.getSystemInfoSync().platform == "ios") {
    jpushModule.requestNotificationAuthorization((result: any) => {
      let status = result.status;
      if (status < 2) {
        noticMsgTool();
      }
    });
  } else {
    jpushModule.isNotificationEnabled((result: any) => {
      //判断android是否打开权限
      if (result.code == 0) {
        //如果为0则表示 未打开通知权限
        noticMsgTool();
      }
    });
  }
}
function noticMsgTool() {
  if (uni.getSystemInfoSync().platform == "ios") {
    //苹果打开对应的通知栏
    uni.showModal({
      title: "通知权限开启提醒",
      content: "您还没有开启通知权限，无法接受到消息通知，请前往设置！",
      showCancel: false,
      confirmText: "去设置",
      success: function (res) {
        if (res.confirm) {
          var app = plus.ios.invoke("UIApplication", "sharedApplication");
          var setting = plus.ios.invoke("NSURL", "URLWithString:", "app-settings:");
          plus.ios.invoke(app, "openURL:", setting);
          plus.ios.deleteObject(setting);
          plus.ios.deleteObject(app);
        }
      }
    });
  } else {
    uni.showModal({
      title: "通知权限开启提醒",
      content: "您还没有开启通知权限，无法接受到消息通知，请前往设置！",
      showCancel: false,
      confirmText: "去设置",
      success: function (res) {
        if (res.confirm) {
        }
      }
    });
  }
}

onLaunch(() => {
  // #ifdef APP-PLUS
  jpushModule.initJPushService();
  jpushModule.setLoggerEnable(true);
  plus.screen.lockOrientation("portrait-primary");
  // 设置别名
  jpushModule.setAlias({
    alias: "123232",
    sequence: 1
  });
  //监听 极光推送连接状态
  getNotificationEnabled();
  jpushModule.addConnectEventListener((result: any) => {
    let connectEnable = result.connectEnable;
    uni.$emit("connectStatusChange", connectEnable);
  });
  jpushModule.addNotificationListener((result: any) => {
    //极光推送的消息通知回调
    jpushModule.setBadge(0);
    plus.runtime.setBadgeNumber(0);
    let notificationEventType = result.notificationEventType;
    console.log("通知", result, notificationEventType);
    // 点击事件
    if (notificationEventType == "notificationOpened") {
      console.log("点击了-----", result);
    }
  });

  jpushModule.addCustomMessageListener((result: any) => {
    // let type = result.type;
    // let messageType = result.messageType;
    // let content = result.content;
    console.log("custom-----", result);
    uni.showToast({
      icon: "none",
      title: JSON.stringify(result),
      duration: 3000
    });
  });

  jpushModule.addLocalNotificationListener((result: any) => {
    // let messageID = result.messageID;
    // let title = result.title;
    // let content = result.content;
    // let extras = result.extras;
    console.log("custom-----", result);
    uni.showToast({
      icon: "none",
      title: JSON.stringify(result),
      duration: 3000
    });
  });

  uni.$on("connectStatusChange", (connectStatus) => {
    var connectStr = "";
    if (connectStatus == true) {
      connectStr = "已连接";
      getRegistrationID();
    } else {
      connectStr = "未连接";
    }
    console.log("监听到了连接状态变化 --- ", connectStr);
  });
  jpushModule.isPushStopped((res: any) => {
    // code   0已停止推送  1未停止推送
    const { code } = res;
    console.log(res, "安卓连接状态", code);
  });

  midtabBarClick();
  //#endif
});

function midtabBarClick() {
  // 点击中间导航按钮的点击事件
  uni.onTabBarMidButtonTap(() => {
    console.log("中间的点击了");
  });
}
</script>

<style lang="scss">
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
/* #ifdef APP-NVUE */
@import "./tmui/scss/nvue.css";
/* #endif */
/* #ifndef APP-NVUE */
@import "./tmui/scss/noNvue.css";
/* #endif */
@import "@/style/common.scss";
@import "static/font/iconfont.css";
@import "static/font/iconcolor.css";
</style>
